package com.zzptc.lzh.myapp26.Fragment;

import android.content.Context;
import android.content.Intent;
import android.content.SharedPreferences;
import android.net.Uri;
import android.os.Bundle;

import androidx.fragment.app.Fragment;

import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.Button;
import android.widget.EditText;
import android.widget.Toast;

import com.zzptc.lzh.myapp26.Bean.User;
import com.zzptc.lzh.myapp26.LoginActivity;
import com.zzptc.lzh.myapp26.R;

import org.litepal.LitePal;

public class ThirdFragment extends Fragment {
    private EditText et_oldpsd,et_newpsd;
    private Button bt_changepsd;
    public ThirdFragment() {
    }

    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
        // Inflate the layout for this fragment
        View view=inflater.inflate(R.layout.fragment_third,container,false);
        initView(view);
        setListener();
        return view;
    }

    private void initView(View view) {
        et_oldpsd=view.findViewById(R.id.et_oldpsd);
        et_newpsd=view.findViewById(R.id.et_newpsd);
        bt_changepsd=view.findViewById(R.id.bt_changepsd);
    }
    private void setListener() {
        bt_changepsd.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                String psd_old=et_oldpsd.getText().toString().trim();
                String psd_new=et_newpsd.getText().toString().trim();
                SharedPreferences sp=getActivity().getSharedPreferences("userinfo",Context.MODE_PRIVATE);
                int uid=sp.getInt("userid",1);
                String password=sp.getString("password","");
                if(psd_old.equals(password)){
                    User user= LitePal.find(User.class,uid);
                    user.setPassword(psd_new);
                    user.save();
                    showToast("修改密码成功，请重新登录");
                    Intent intent=new Intent(getContext(), LoginActivity.class);
                    startActivity(intent);
                    getActivity().finish();
                }else{
                    showToast("原密码错误");
                }
            }
        });

    }
    private void showToast(String str){
        Toast.makeText(getContext(), str, Toast.LENGTH_SHORT).show();
    }

}
